#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <QSoundEffect>
#include "mypushbutton.h"

MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);
    //配置主场景
    //设置固定大小
    setFixedSize(320,588);
    //设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));
    //设置标题
    setWindowTitle("翻金币主场景");
    //退出按钮
    connect(ui->actionQuit,&QAction::triggered,[=](){
        this->close();
    });
    //音效
    QSoundEffect * startSound= new QSoundEffect(this);
    startSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
    //开始按钮
    MyPushButton * startBtn=new MyPushButton(":/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width()*.5-startBtn->width()*.5,this->height()*.7);
    //选关卡场景
    this->chooseScene = new ChooseLevelScene;
    connect(startBtn,&QPushButton::clicked,[=](){
        startSound->play();
        qDebug()<<startSound->volume();
        qDebug()<<"点击开始按钮";
        //弹起特效
        startBtn->zoom1();
        startBtn->zoom2();
        //延时进入
        QTimer::singleShot(500,this,[=](){
            //自身隐藏，显示选择关卡的场景
            this->hide();
            //显示
            this->chooseScene->setGeometry(this->geometry());
            this->chooseScene->show();
        });
    });
    //监听选择关卡的返回按钮的信号
    connect(chooseScene,&ChooseLevelScene::choseSceneBack,this,[=](){
       this->chooseScene->hide();
       this->setGeometry(this->chooseScene->geometry());
       this->show();//重新显示
    });

}

void MainScene::paintEvent(QPaintEvent *)
{
    //创建画家,指定绘图设备为Widget
    QPainter painter(this);
    //创建QPixmap对象
    QPixmap pix;
    ///加载背景图片///
    pix.load(":/res/PlayLevelSceneBg.png");
    //绘制背景图
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    ///加载标题///
    pix.load(":/res/Title.png");
    //缩放图片
    pix = pix.scaled(pix.width()*.5,pix.height()*.5);
    //绘制
    painter.drawPixmap(10,30,pix);
}

MainScene::~MainScene()
{
    delete ui;
}

